import type { ProfileInfo } from '@/types/user'
import { getLocalToken, removeLocalToken, setLocalToken } from '@/utils/auth'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('user', () => {
  const token = ref(getLocalToken() || '')
  const profile = ref<ProfileInfo>({} as ProfileInfo)

  const setToken = (val: string) => {
    token.value = val
    setLocalToken(val)
  }

  const removeToken = () => {
    token.value = ''
    removeLocalToken()
  }

  const setUser = async (profileInfo: ProfileInfo) => {
    profile.value = profileInfo
  }

  return { token, setToken, removeToken, profile, setUser }
})
